<?php

namespace app\web\controller;
use app\web\model\SmsMassModel;
use app\web\model\AgentModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 短信群发
 */
class SmsMassController extends StoreBaseController
{
 	protected $model;
	protected $agentmodel;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new SmsMassModel();
		$this->agentmodel = new AgentModel();		
    }
	
	public function members(){
		$store = session('storeInfo');	
		$params=input("");
		$where['status']=1;
		if(isset($params['mobile'])&&$params['mobile']){
			$where['mobile'] = ['like', '%'.$param['mobile'].'%'];
		}
		if(isset($params['member_no'])&&$params['member_no']){
			$where['member_no'] = ['like', '%'.$param['member_no'].'%'];
		}
		if(isset($params['membername'])&&$params['membername']){
			$where['membername'] = ['like', '%'.$param['membername'].'%'];
		}
		if(isset($params['m1'])&&$params['m1']){//生日开始
			//$m1=$date('Y-m-d',strtotime("2017-".$params['m1']));
			$where['CONCAT("2017-",birthday_monthday)'] = [">=","2017-".$params['m1']];
		}
		if(isset($params['m2'])&&$params['m2']){//生日结束
			$where['CONCAT("2017-",birthday_monthday)'] = ["<=","2017-".$params['m2']];
		}
		if(isset($params['c1'])&&$params['c1']){//创建开始
			$where['reg_date'] = [">=",$params['c1']];
		}
		if(isset($params['c2'])&&$params['c2']){//
			$where['reg_date'] = ["<=",$params['c2']];
		}
		$list=db("line_member")->where($where)->select();
		$this->assign("list",$list);
		return $this->fetch();
	}
    /**
     * 添加群发记录
	 * @param int $page
     */
    public function index(){
    	$store = session('storeInfo');	
		$this->assign('smscount',$store['smscount']);
        if(request()->isPost()){
        	$param = input('post.');
			$param['store_id'] = $store['id'];
			$param['store_name'] = $store['store_name'];
			$param['add_time'] = now_time();	
			//号码集合
			$mobile_list = explode("\n",$param['mobile']);
			$size=sizeof($mobile_list);
			if($store['smscount']<$size){
				$this->success('短信剩余次数不足', url('sms_mass/index'));
			}			
			$param['num'] =$size ;
			$param['mobile'] = str_replace("\n", ",", $param['mobile']);
			//发送短信
			send_sms($param['mobile'],'SMS_25225072','','','用户!!!'.$param['content']);
			if ($this->model->allowField(true)->save($param) !== false) {							
				Db::name('mall_agent')->where('id', $store['id'])->setDec('smscount', $param['num']);
                $this->success('发送成功', url('sms_mass/index'));
            }else{
                $this->error('发送失败');
            }
        }
		return $this->fetch();
    }
}